using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Tino.Controls
{
    /// <summary>
    /// Create a new instance of AdvancedRichTextBox.
    /// If a formatting action fails, nothing is thrown. In order to catch any exceptions that
    /// may occur, handle the event FormattingError in the calling code.
    /// To save the content of the RichTextBox wherever you want, handle the event Save in the
    /// calling code. The SaveEventArgs has a property called RtfText that contains the actual
    /// Rtf text of the RichTextBox
    /// </summary>
    public class AdvancedRichTextBox : UserControl, IRtfWritter
    {
        // Const
        const int indentThreshold = 20;

        // Public events
        public event FormattingErrorEventHandler FormattingError;
        public event SaveEventHandler Save;

        #region Private controls

        private Panel contentPanel;
        private RichTextBox richTextBox;
        private ToolStrip toolStrip;
        private ToolStripButton saveToolStripButton;
        private ToolStripButton undoToolStripButton;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripButton cutToolStripButton;
        private ToolStripButton copyToolStripButton;
        private ToolStripButton pasteToolStripButton;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripButton pickFontToolStripButton;
        private ToolStripButton pickColorToolStripButton;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripButton alignLeftToolStripButton;
        private ToolStripButton alignCenterToolStripButton;
        private ToolStripButton alignRightToolStripButton;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripButton bulletToolStripButton;
        private ToolStripSeparator toolStripSeparator5;
        private ToolStripButton boldToolStripButton;
        private ToolStripButton italicToolStripButton;
        private ToolStripButton underlineToolStripButton;
        private ColorDialog colorDialog;
        private FontDialog fontDialog;
        private ContextMenuStrip contextMenuStrip;
        private IContainer components;
        private ToolStripMenuItem undoToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator6;
        private ToolStripMenuItem pickFontToolStripMenuItem;
        private ToolStripMenuItem pickColorToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator7;
        private ToolStripMenuItem cutToolStripMenuItem;
        private ToolStripMenuItem copyToolStripMenuItem;
        private ToolStripMenuItem pasteToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator8;
        private ToolStripMenuItem selectAllToolStripMenuItem;
        private ToolStripMenuItem boldToolStripMenuItem;
        private ToolStripMenuItem italicToolStripMenuItem;
        private ToolStripMenuItem underlineToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator9;
        private ToolStripMenuItem openFontDialogoolStripMenuItem;
        private ToolStripSeparator toolStripSeparator10;
        private ToolStripButton indentToolStripButton;
        private ToolStripButton outdentToolStripButton;
        private ToolStripButton wordWrapToolStripButton;
        private ToolStripSeparator toolStripSeparator11;
        private TableLayoutPanel mainTable;

        #endregion

        // Public properties
        public string RtfText
        {
            get { return richTextBox.Rtf; }
            set { richTextBox.Rtf = value; }
        }

        public bool ReadOnly
        {
            get { return richTextBox.ReadOnly; }
            set 
            {
                richTextBox.ReadOnly = value;
                richTextBox.BackColor = Color.White;
            }
        }

        public new Font DefaultFont
        {
            get { return richTextBox.Font; }
            set { richTextBox.Font = value; }
        }

        public bool ShowToolStripButton
        {
            get { return toolStrip.Visible; }
            set 
            {
                toolStrip.Visible = value;
                // Resize first row according to visibility status
                mainTable.RowStyles[0] = new RowStyle(SizeType.Absolute, (value ? 25 : 0));
            }
        }

        public bool ShowSaveButton
        {
            get { return saveToolStripButton.Visible; }
            set { saveToolStripButton.Visible = value; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public AdvancedRichTextBox()
        {
            InitializeComponent();
            setVocables();
        }

        public virtual void OnFormattingError(FormattingErrorEventArgs e)
        {
            if (FormattingError != null)
                FormattingError(this, e);
        }

        public virtual void OnSave(SaveEventArgs e)
        {
            if (Save != null)
                Save(this, e);
        }

        public void Copy()
        {
            richTextBox.Copy();
        }

        public void SelectAll()
        {
            richTextBox.SelectAll();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AdvancedRichTextBox));
            this.mainTable = new System.Windows.Forms.TableLayoutPanel();
            this.contentPanel = new System.Windows.Forms.Panel();
            this.richTextBox = new System.Windows.Forms.RichTextBox();
            this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.undoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
            this.pickFontToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.boldToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.italicToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.underlineToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator();
            this.openFontDialogoolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.pickColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator7 = new System.Windows.Forms.ToolStripSeparator();
            this.cutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.copyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.pasteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator8 = new System.Windows.Forms.ToolStripSeparator();
            this.selectAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStrip = new System.Windows.Forms.ToolStrip();
            this.saveToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.undoToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.cutToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.copyToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.pasteToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.pickFontToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.pickColorToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.alignLeftToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.alignCenterToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.alignRightToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.bulletToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.boldToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.italicToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.underlineToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator10 = new System.Windows.Forms.ToolStripSeparator();
            this.indentToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.outdentToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator11 = new System.Windows.Forms.ToolStripSeparator();
            this.wordWrapToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.colorDialog = new System.Windows.Forms.ColorDialog();
            this.fontDialog = new System.Windows.Forms.FontDialog();
            this.mainTable.SuspendLayout();
            this.contentPanel.SuspendLayout();
            this.contextMenuStrip.SuspendLayout();
            this.toolStrip.SuspendLayout();
            this.SuspendLayout();
            // 
            // mainTable
            // 
            this.mainTable.ColumnCount = 1;
            this.mainTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.mainTable.Controls.Add(this.contentPanel, 0, 1);
            this.mainTable.Controls.Add(this.toolStrip, 0, 0);
            this.mainTable.Dock = System.Windows.Forms.DockStyle.Fill;
            this.mainTable.Location = new System.Drawing.Point(0, 0);
            this.mainTable.Margin = new System.Windows.Forms.Padding(0);
            this.mainTable.Name = "mainTable";
            this.mainTable.RowCount = 2;
            this.mainTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
            this.mainTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.mainTable.Size = new System.Drawing.Size(451, 170);
            this.mainTable.TabIndex = 0;
            // 
            // contentPanel
            // 
            this.contentPanel.BackColor = System.Drawing.Color.White;
            this.contentPanel.Controls.Add(this.richTextBox);
            this.contentPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.contentPanel.Location = new System.Drawing.Point(0, 25);
            this.contentPanel.Margin = new System.Windows.Forms.Padding(0);
            this.contentPanel.Name = "contentPanel";
            this.contentPanel.Padding = new System.Windows.Forms.Padding(4);
            this.contentPanel.Size = new System.Drawing.Size(451, 145);
            this.contentPanel.TabIndex = 0;
            // 
            // richTextBox
            // 
            this.richTextBox.BackColor = System.Drawing.SystemColors.Window;
            this.richTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.richTextBox.ContextMenuStrip = this.contextMenuStrip;
            this.richTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox.Location = new System.Drawing.Point(4, 4);
            this.richTextBox.Name = "richTextBox";
            this.richTextBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
            this.richTextBox.ShowSelectionMargin = true;
            this.richTextBox.Size = new System.Drawing.Size(443, 137);
            this.richTextBox.TabIndex = 0;
            this.richTextBox.Text = "";
            this.richTextBox.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox_LinkClicked);
            this.richTextBox.SelectionChanged += new System.EventHandler(this.richTextBox_SelectionChanged);
            // 
            // contextMenuStrip
            // 
            this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.undoToolStripMenuItem,
            this.toolStripSeparator6,
            this.pickFontToolStripMenuItem,
            this.pickColorToolStripMenuItem,
            this.toolStripSeparator7,
            this.cutToolStripMenuItem,
            this.copyToolStripMenuItem,
            this.pasteToolStripMenuItem,
            this.toolStripSeparator8,
            this.selectAllToolStripMenuItem});
            this.contextMenuStrip.Name = "contextMenuStrip";
            this.contextMenuStrip.Size = new System.Drawing.Size(151, 176);
            // 
            // undoToolStripMenuItem
            // 
            this.undoToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("undoToolStripMenuItem.Image")));
            this.undoToolStripMenuItem.Name = "undoToolStripMenuItem";
            this.undoToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.undoToolStripMenuItem.Text = "Undo";
            this.undoToolStripMenuItem.Click += new System.EventHandler(this.undoToolStripMenuItem_Click);
            // 
            // toolStripSeparator6
            // 
            this.toolStripSeparator6.Name = "toolStripSeparator6";
            this.toolStripSeparator6.Size = new System.Drawing.Size(147, 6);
            // 
            // pickFontToolStripMenuItem
            // 
            this.pickFontToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.boldToolStripMenuItem,
            this.italicToolStripMenuItem,
            this.underlineToolStripMenuItem,
            this.toolStripSeparator9,
            this.openFontDialogoolStripMenuItem});
            this.pickFontToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("pickFontToolStripMenuItem.Image")));
            this.pickFontToolStripMenuItem.Name = "pickFontToolStripMenuItem";
            this.pickFontToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.pickFontToolStripMenuItem.Text = "Font";
            // 
            // boldToolStripMenuItem
            // 
            this.boldToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("boldToolStripMenuItem.Image")));
            this.boldToolStripMenuItem.Name = "boldToolStripMenuItem";
            this.boldToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
            this.boldToolStripMenuItem.Text = "Bold";
            this.boldToolStripMenuItem.Click += new System.EventHandler(this.boldToolStripMenuItem_Click);
            // 
            // italicToolStripMenuItem
            // 
            this.italicToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("italicToolStripMenuItem.Image")));
            this.italicToolStripMenuItem.Name = "italicToolStripMenuItem";
            this.italicToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
            this.italicToolStripMenuItem.Text = "Italic";
            this.italicToolStripMenuItem.Click += new System.EventHandler(this.italicToolStripMenuItem_Click);
            // 
            // underlineToolStripMenuItem
            // 
            this.underlineToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("underlineToolStripMenuItem.Image")));
            this.underlineToolStripMenuItem.Name = "underlineToolStripMenuItem";
            this.underlineToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
            this.underlineToolStripMenuItem.Text = "Underline";
            this.underlineToolStripMenuItem.Click += new System.EventHandler(this.underlineToolStripMenuItem_Click);
            // 
            // toolStripSeparator9
            // 
            this.toolStripSeparator9.Name = "toolStripSeparator9";
            this.toolStripSeparator9.Size = new System.Drawing.Size(130, 6);
            // 
            // openFontDialogoolStripMenuItem
            // 
            this.openFontDialogoolStripMenuItem.Name = "openFontDialogoolStripMenuItem";
            this.openFontDialogoolStripMenuItem.Size = new System.Drawing.Size(133, 22);
            this.openFontDialogoolStripMenuItem.Text = "Choose...";
            this.openFontDialogoolStripMenuItem.Click += new System.EventHandler(this.openFontDialogoolStripMenuItem_Click);
            // 
            // pickColorToolStripMenuItem
            // 
            this.pickColorToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("pickColorToolStripMenuItem.Image")));
            this.pickColorToolStripMenuItem.Name = "pickColorToolStripMenuItem";
            this.pickColorToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.pickColorToolStripMenuItem.Text = "Color";
            this.pickColorToolStripMenuItem.Click += new System.EventHandler(this.pickColorToolStripMenuItem_Click);
            // 
            // toolStripSeparator7
            // 
            this.toolStripSeparator7.Name = "toolStripSeparator7";
            this.toolStripSeparator7.Size = new System.Drawing.Size(147, 6);
            // 
            // cutToolStripMenuItem
            // 
            this.cutToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("cutToolStripMenuItem.Image")));
            this.cutToolStripMenuItem.Name = "cutToolStripMenuItem";
            this.cutToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
            this.cutToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.cutToolStripMenuItem.Text = "Cut";
            this.cutToolStripMenuItem.Click += new System.EventHandler(this.cutToolStripMenuItem_Click);
            // 
            // copyToolStripMenuItem
            // 
            this.copyToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("copyToolStripMenuItem.Image")));
            this.copyToolStripMenuItem.Name = "copyToolStripMenuItem";
            this.copyToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));
            this.copyToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.copyToolStripMenuItem.Text = "Copy";
            this.copyToolStripMenuItem.Click += new System.EventHandler(this.copyToolStripMenuItem_Click);
            // 
            // pasteToolStripMenuItem
            // 
            this.pasteToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("pasteToolStripMenuItem.Image")));
            this.pasteToolStripMenuItem.Name = "pasteToolStripMenuItem";
            this.pasteToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.P)));
            this.pasteToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.pasteToolStripMenuItem.Text = "Paste";
            this.pasteToolStripMenuItem.Click += new System.EventHandler(this.pasteToolStripMenuItem_Click);
            // 
            // toolStripSeparator8
            // 
            this.toolStripSeparator8.Name = "toolStripSeparator8";
            this.toolStripSeparator8.Size = new System.Drawing.Size(147, 6);
            // 
            // selectAllToolStripMenuItem
            // 
            this.selectAllToolStripMenuItem.Name = "selectAllToolStripMenuItem";
            this.selectAllToolStripMenuItem.Size = new System.Drawing.Size(150, 22);
            this.selectAllToolStripMenuItem.Text = "Select all";
            this.selectAllToolStripMenuItem.Click += new System.EventHandler(this.selectAllToolStripMenuItem_Click);
            // 
            // toolStrip
            // 
            this.toolStrip.Dock = System.Windows.Forms.DockStyle.Fill;
            this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveToolStripButton,
            this.undoToolStripButton,
            this.toolStripSeparator1,
            this.cutToolStripButton,
            this.copyToolStripButton,
            this.pasteToolStripButton,
            this.toolStripSeparator2,
            this.pickFontToolStripButton,
            this.pickColorToolStripButton,
            this.toolStripSeparator3,
            this.alignLeftToolStripButton,
            this.alignCenterToolStripButton,
            this.alignRightToolStripButton,
            this.toolStripSeparator4,
            this.bulletToolStripButton,
            this.toolStripSeparator5,
            this.boldToolStripButton,
            this.italicToolStripButton,
            this.underlineToolStripButton,
            this.toolStripSeparator10,
            this.indentToolStripButton,
            this.outdentToolStripButton,
            this.toolStripSeparator11,
            this.wordWrapToolStripButton});
            this.toolStrip.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
            this.toolStrip.Location = new System.Drawing.Point(0, 0);
            this.toolStrip.Margin = new System.Windows.Forms.Padding(0, 0, 0, 2);
            this.toolStrip.Name = "toolStrip";
            this.toolStrip.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.toolStrip.Size = new System.Drawing.Size(451, 23);
            this.toolStrip.TabIndex = 1;
            this.toolStrip.Text = "toolStrip1";
            // 
            // saveToolStripButton
            // 
            this.saveToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.saveToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("saveToolStripButton.Image")));
            this.saveToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.saveToolStripButton.Name = "saveToolStripButton";
            this.saveToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.saveToolStripButton.Click += new System.EventHandler(this.saveToolStripButton_Click);
            // 
            // undoToolStripButton
            // 
            this.undoToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.undoToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("undoToolStripButton.Image")));
            this.undoToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.undoToolStripButton.Name = "undoToolStripButton";
            this.undoToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.undoToolStripButton.Click += new System.EventHandler(this.undoToolStripButton_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 23);
            // 
            // cutToolStripButton
            // 
            this.cutToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.cutToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("cutToolStripButton.Image")));
            this.cutToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.cutToolStripButton.Name = "cutToolStripButton";
            this.cutToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.cutToolStripButton.Click += new System.EventHandler(this.cutToolStripButton_Click);
            // 
            // copyToolStripButton
            // 
            this.copyToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.copyToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("copyToolStripButton.Image")));
            this.copyToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.copyToolStripButton.Name = "copyToolStripButton";
            this.copyToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.copyToolStripButton.Click += new System.EventHandler(this.copyToolStripButton_Click);
            // 
            // pasteToolStripButton
            // 
            this.pasteToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.pasteToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("pasteToolStripButton.Image")));
            this.pasteToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.pasteToolStripButton.Name = "pasteToolStripButton";
            this.pasteToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.pasteToolStripButton.Click += new System.EventHandler(this.pasteToolStripButton_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 23);
            // 
            // pickFontToolStripButton
            // 
            this.pickFontToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.pickFontToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("pickFontToolStripButton.Image")));
            this.pickFontToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.pickFontToolStripButton.Name = "pickFontToolStripButton";
            this.pickFontToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.pickFontToolStripButton.Click += new System.EventHandler(this.pickFontToolStripButton_Click);
            // 
            // pickColorToolStripButton
            // 
            this.pickColorToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.pickColorToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("pickColorToolStripButton.Image")));
            this.pickColorToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.pickColorToolStripButton.Name = "pickColorToolStripButton";
            this.pickColorToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.pickColorToolStripButton.Click += new System.EventHandler(this.pickColorToolStripButton_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 23);
            // 
            // alignLeftToolStripButton
            // 
            this.alignLeftToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.alignLeftToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("alignLeftToolStripButton.Image")));
            this.alignLeftToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.alignLeftToolStripButton.Name = "alignLeftToolStripButton";
            this.alignLeftToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.alignLeftToolStripButton.Click += new System.EventHandler(this.alignLeftToolStripButton_Click);
            // 
            // alignCenterToolStripButton
            // 
            this.alignCenterToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.alignCenterToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("alignCenterToolStripButton.Image")));
            this.alignCenterToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.alignCenterToolStripButton.Name = "alignCenterToolStripButton";
            this.alignCenterToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.alignCenterToolStripButton.Click += new System.EventHandler(this.alignCenterToolStripButton_Click);
            // 
            // alignRightToolStripButton
            // 
            this.alignRightToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.alignRightToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("alignRightToolStripButton.Image")));
            this.alignRightToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.alignRightToolStripButton.Name = "alignRightToolStripButton";
            this.alignRightToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.alignRightToolStripButton.Click += new System.EventHandler(this.alignRightoolStripButton_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 23);
            // 
            // bulletToolStripButton
            // 
            this.bulletToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.bulletToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("bulletToolStripButton.Image")));
            this.bulletToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.bulletToolStripButton.Name = "bulletToolStripButton";
            this.bulletToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.bulletToolStripButton.Click += new System.EventHandler(this.bulletToolStripButton_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            this.toolStripSeparator5.Size = new System.Drawing.Size(6, 23);
            // 
            // boldToolStripButton
            // 
            this.boldToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.boldToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("boldToolStripButton.Image")));
            this.boldToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.boldToolStripButton.Name = "boldToolStripButton";
            this.boldToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.boldToolStripButton.Click += new System.EventHandler(this.boldToolStripButton_Click);
            // 
            // italicToolStripButton
            // 
            this.italicToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.italicToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("italicToolStripButton.Image")));
            this.italicToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.italicToolStripButton.Name = "italicToolStripButton";
            this.italicToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.italicToolStripButton.Click += new System.EventHandler(this.italicToolStripButton_Click);
            // 
            // underlineToolStripButton
            // 
            this.underlineToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.underlineToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("underlineToolStripButton.Image")));
            this.underlineToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.underlineToolStripButton.Name = "underlineToolStripButton";
            this.underlineToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.underlineToolStripButton.Click += new System.EventHandler(this.underLineToolStripButton_Click);
            // 
            // toolStripSeparator10
            // 
            this.toolStripSeparator10.Name = "toolStripSeparator10";
            this.toolStripSeparator10.Size = new System.Drawing.Size(6, 23);
            // 
            // indentToolStripButton
            // 
            this.indentToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.indentToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("indentToolStripButton.Image")));
            this.indentToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.indentToolStripButton.Name = "indentToolStripButton";
            this.indentToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.indentToolStripButton.Click += new System.EventHandler(this.indentToolStripButton_Click);
            // 
            // outdentToolStripButton
            // 
            this.outdentToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.outdentToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("outdentToolStripButton.Image")));
            this.outdentToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.outdentToolStripButton.Name = "outdentToolStripButton";
            this.outdentToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.outdentToolStripButton.Click += new System.EventHandler(this.outdentToolStripButton_Click);
            // 
            // toolStripSeparator11
            // 
            this.toolStripSeparator11.Name = "toolStripSeparator11";
            this.toolStripSeparator11.Size = new System.Drawing.Size(6, 23);
            // 
            // wordWrapToolStripButton
            // 
            this.wordWrapToolStripButton.Checked = true;
            this.wordWrapToolStripButton.CheckState = System.Windows.Forms.CheckState.Checked;
            this.wordWrapToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.wordWrapToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("wordWrapToolStripButton.Image")));
            this.wordWrapToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.wordWrapToolStripButton.Name = "wordWrapToolStripButton";
            this.wordWrapToolStripButton.Size = new System.Drawing.Size(23, 20);
            this.wordWrapToolStripButton.Click += new System.EventHandler(this.wordWrapToolStripButton_Click);
            // 
            // AdvancedRichTextBox
            // 
            this.Controls.Add(this.mainTable);
            this.Margin = new System.Windows.Forms.Padding(0);
            this.Name = "AdvancedRichTextBox";
            this.Size = new System.Drawing.Size(451, 170);
            this.mainTable.ResumeLayout(false);
            this.mainTable.PerformLayout();
            this.contentPanel.ResumeLayout(false);
            this.contextMenuStrip.ResumeLayout(false);
            this.toolStrip.ResumeLayout(false);
            this.toolStrip.PerformLayout();
            this.ResumeLayout(false);

        }

        /// <summary>
        /// Refresh the states of the ToolStrip buttons depending on the selection.
        /// </summary>
        void refreshUI()
        {
            alignLeftToolStripButton.Checked = (richTextBox.SelectionAlignment == HorizontalAlignment.Left);
            alignCenterToolStripButton.Checked = (richTextBox.SelectionAlignment == HorizontalAlignment.Center);
            alignRightToolStripButton.Checked = (richTextBox.SelectionAlignment == HorizontalAlignment.Right);

            bulletToolStripButton.Checked = (richTextBox.SelectionBullet);

            if (richTextBox.SelectionFont != null)
            {
                boldToolStripButton.Checked = (richTextBox.SelectionFont.Bold);
                italicToolStripButton.Checked = (richTextBox.SelectionFont.Italic);
                underlineToolStripButton.Checked = (richTextBox.SelectionFont.Underline);
            }

            wordWrapToolStripButton.Checked = (richTextBox.WordWrap);
        }

        void save()
        {
            // So that the actual save method is delegated to the caller
            OnSave(new SaveEventArgs(richTextBox.Rtf));
        }

        void undo()
        {
            try
            {
                richTextBox.Undo();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void cut()
        {
            try
            {
                richTextBox.Cut();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void copy()
        {
            try
            {
                richTextBox.Copy();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void paste()
        {
            try
            {
                richTextBox.Paste();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void chooseFont()
        {
            try
            {
                fontDialog.Font = richTextBox.SelectionFont;
                if (fontDialog.ShowDialog() != DialogResult.Cancel)
                {
                    richTextBox.SelectionFont = fontDialog.Font;
                    refreshUI();
                }
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void chooseColor()
        {
            try
            {
                colorDialog.Color = richTextBox.SelectionColor;
                if (colorDialog.ShowDialog() != DialogResult.Cancel)
                {
                    richTextBox.SelectionColor = colorDialog.Color;
                    refreshUI();
                }
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void setAlign(HorizontalAlignment align)
        {
            try
            {
                richTextBox.SelectionAlignment = align;
                refreshUI();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void setBullet()
        {
            try
            {
                richTextBox.SelectionBullet = !richTextBox.SelectionBullet;
                refreshUI();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void setBold()
        {
            try
            {
                if (richTextBox.SelectionFont != null)
                {
                    Font currentFont = richTextBox.SelectionFont;
                    FontStyle newFontStyle;
                    newFontStyle = richTextBox.SelectionFont.Style ^ FontStyle.Bold;
                    richTextBox.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size,
                        newFontStyle);

                    refreshUI();
                }
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void setItalic()
        {
            try
            {
                if (richTextBox.SelectionFont != null)
                {
                    Font currentFont = richTextBox.SelectionFont;
                    FontStyle newFontStyle;
                    newFontStyle = richTextBox.SelectionFont.Style ^ FontStyle.Italic;
                    richTextBox.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size,
                        newFontStyle);

                    refreshUI();
                }
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void setUnderline()
        {
            try
            {
                if (richTextBox.SelectionFont != null)
                {
                    Font currentFont = richTextBox.SelectionFont;
                    FontStyle newFontStyle;
                    newFontStyle = richTextBox.SelectionFont.Style ^ FontStyle.Underline;
                    richTextBox.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size,
                        newFontStyle);

                    refreshUI();
                }
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void indent()
        {
            try
            {
                richTextBox.SelectionIndent += indentThreshold;
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void outdent()
        {
            try
            {
                if ((richTextBox.SelectionIndent - indentThreshold) >= 0)
                    richTextBox.SelectionIndent -= indentThreshold;
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void setWordWrap()
        {
            try
            {
                richTextBox.WordWrap = !richTextBox.WordWrap;
                refreshUI();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        void selectAll()
        {
            try
            {
                richTextBox.SelectAll();
            }
            catch (Exception ex)
            {
                OnFormattingError(new FormattingErrorEventArgs(ex));
            }
        }

        #region RichTextBox event handlers

        void richTextBox_SelectionChanged(object sender, EventArgs e)
        {
            refreshUI();
        }

        void richTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            string target = e.LinkText;
            if (target != null && target.StartsWith("http"))
            {
                System.Diagnostics.Process.Start(target);
            }
        }

        #endregion

        #region Vocables

        void setVocables()
        {
            saveToolStripButton.Text = saveVocable;
            undoToolStripButton.Text = undoVocable;
            undoToolStripMenuItem.Text = undoVocable;
            cutToolStripButton.Text = cutVocable;
            cutToolStripMenuItem.Text = cutVocable;
            copyToolStripButton.Text = copyVocable;
            copyToolStripMenuItem.Text = copyVocable;
            pasteToolStripButton.Text = pasteVocable;
            pasteToolStripMenuItem.Text = pasteVocable;
            pickFontToolStripButton.Text = pickFontVocable;
            pickFontToolStripMenuItem.Text = pickFontVocable;
            pickColorToolStripButton.Text = pickColorVocable;
            pickColorToolStripMenuItem.Text = pickColorVocable;
            alignLeftToolStripButton.Text = alignLeftVocable;
            alignCenterToolStripButton.Text = alignCenterVocable;
            alignRightToolStripButton.Text = alignRightVocable;
            bulletToolStripButton.Text = bulletVocable;
            boldToolStripButton.Text = boldVocable;
            boldToolStripMenuItem.Text = boldVocable;
            italicToolStripButton.Text = italicVocable;
            italicToolStripMenuItem.Text = italicVocable;
            underlineToolStripButton.Text = underlineVocable;
            underlineToolStripMenuItem.Text = underlineVocable;

            openFontDialogoolStripMenuItem.Text = chooseVocable;
            selectAllToolStripMenuItem.Text = selectAllVocable;
        }

        string saveVocable = "Save";
        public string SaveVocable
        {
            get { return saveVocable; }
            set { saveVocable = value; }
        }

        string undoVocable = "Undo";
        public string UndoVocable
        {
            get { return undoVocable; }
            set { undoVocable = value; }
        }

        string cutVocable = "Cut";
        public string CutVocable
        {
            get { return cutVocable; }
            set { cutVocable = value; }
        }

        string copyVocable = "Copy";
        public string CopyVocable
        {
            get { return copyVocable; }
            set { copyVocable = value; }
        }

        string pasteVocable = "Paste";
        public string PasteVocable
        {
            get { return pasteVocable; }
            set { pasteVocable = value; }
        }

        string pickFontVocable = "Choose font...";
        public string PickFontVocable
        {
            get { return pickFontVocable; }
            set { pickFontVocable = value; }
        }

        string chooseVocable = "Choose...";
        public string ChooseVocable
        {
            get { return chooseVocable; }
            set { chooseVocable = value; }
        }

        string pickColorVocable = "Choose color...";
        public string PickColorVocable
        {
            get { return pickColorVocable; }
            set { pickColorVocable = value; }
        }

        string alignLeftVocable = "Left align";
        public string AlignLeftVocable
        {
            get { return alignLeftVocable; }
            set { alignLeftVocable = value; }
        }

        string alignCenterVocable = "Center align";
        public string AlignCenterVocable
        {
            get { return alignCenterVocable; }
            set { alignCenterVocable = value; }
        }

        string alignRightVocable = "Right align";
        public string AlignRightVocable
        {
            get { return alignRightVocable; }
            set { alignRightVocable = value; }
        }

        string bulletVocable = "List";
        public string BulletVocable
        {
            get { return bulletVocable; }
            set { bulletVocable = value; }
        }

        string boldVocable = "Bold";
        public string BoldVocable
        {
            get { return boldVocable; }
            set { boldVocable = value; }
        }

        string italicVocable = "Italic";
        public string ItalicVocable
        {
            get { return italicVocable; }
            set { italicVocable = value; }
        }

        string underlineVocable = "Underline";
        public string UnderlineVocable
        {
            get { return underlineVocable; }
            set { underlineVocable = value; }
        }

        string selectAllVocable = "Select all";
        public string SelectAllVocable
        {
            get { return selectAllVocable; }
            set { selectAllVocable = value; }
        }

        #endregion

        #region ToolStrip event handlers

        void saveToolStripButton_Click(object sender, EventArgs e)
        {
            save();
        }

        void undoToolStripButton_Click(object sender, EventArgs e)
        {
            undo();
        }

        void cutToolStripButton_Click(object sender, EventArgs e)
        {
            cut();
        }

        void copyToolStripButton_Click(object sender, EventArgs e)
        {
            copy();
        }

        void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            paste();
        }

        void pickFontToolStripButton_Click(object sender, EventArgs e)
        {
            chooseFont();
        }

        void pickColorToolStripButton_Click(object sender, EventArgs e)
        {
            chooseColor();
        }

        void alignLeftToolStripButton_Click(object sender, EventArgs e)
        {
            setAlign(HorizontalAlignment.Left);
        }

        void alignCenterToolStripButton_Click(object sender, EventArgs e)
        {
            setAlign(HorizontalAlignment.Center);
        }

        void alignRightoolStripButton_Click(object sender, EventArgs e)
        {
            setAlign(HorizontalAlignment.Right);
        }

        void bulletToolStripButton_Click(object sender, EventArgs e)
        {
            setBullet();
        }

        void boldToolStripButton_Click(object sender, EventArgs e)
        {
            setBold();
        }

        void italicToolStripButton_Click(object sender, EventArgs e)
        {
            setItalic();
        }

        void underLineToolStripButton_Click(object sender, EventArgs e)
        {
            setUnderline();
        }

        void indentToolStripButton_Click(object sender, EventArgs e)
        {
            indent();
        }

        void outdentToolStripButton_Click(object sender, EventArgs e)
        {
            outdent();
        }

        void wordWrapToolStripButton_Click(object sender, EventArgs e)
        {
            setWordWrap();
        }

        #endregion

        #region ContextMenuStrip event handlers

        void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            undo();
        }

        void boldToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setBold();
        }

        void italicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setItalic();
        }

        void underlineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setUnderline();
        }

        void openFontDialogoolStripMenuItem_Click(object sender, EventArgs e)
        {
            chooseFont();
        }

        void pickColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            chooseColor();
        }

        void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cut();
        }

        void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            copy();
        }

        void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            paste();
        }

        void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            selectAll();
        }

        #endregion 
    }

    #region AdvancedRichTextBox events definition

    public delegate void FormattingErrorEventHandler(object sender, FormattingErrorEventArgs e);

    public class FormattingErrorEventArgs : EventArgs
    {
        Exception exception;

        public Exception Exception { get { return exception; } }

        public FormattingErrorEventArgs(Exception Ex)
        {
            exception = Ex;
        }
    }

    public delegate void SaveEventHandler(object sender, SaveEventArgs e);

    public class SaveEventArgs : EventArgs
    {
        string rtfText;

        public string RtfText { get { return rtfText; } }

        public SaveEventArgs(string RtfText)
        {
            rtfText = RtfText;
        }
    }

    #endregion
}